#include "Dictionary.h"
#include <fstream>

Dictionary::Dictionary(const char* dictionaryPath)
{
    std::ifstream input(dictionaryPath);
    if(input.is_open())
    {
        while(input.peek() != std::char_traits<wchar_t>::eof())
        {
            std::string line;
            getline(input, line);
            Entry entry(line);
            this->entries_.push_back(entry);
        }
    }
}

//void Dictionary::addEntry(std::string entry, int page)
//{
//    Entry* found = containsEntry(entry);
//
//    if(found)
//        (*found).pages_.push_back(page);
//    else
//    {
//        Entry newEntry(entry);
//        newEntry.pages_.push_back(page);
//        this->entries_.push_back(newEntry);
//    }
//}

Entry* Dictionary::containsEntry(std::string entry)
{
    Entry* found = 0;
    for(std::vector<Entry>::iterator it = this->entries_.begin(); it != this->entries_.end(); it++)
    {
        if((*it).value_ == entry)
        {
            found = &(*it);
            break;
        }
    }

    return found;
}

std::vector<Entry>& Dictionary::getEntries()
{
    return this->entries_;
}

const std::vector<Entry>& Dictionary::getEntries() const
{
    return this->entries_;
}

std::ostream& operator<<(std::ostream& output, const Entry& entry)
{
    if (!entry.pages_.empty())
    {
        output << entry.value_ << ": ";
        std::vector<int> pages = entry.pages_;
        std::vector<int>::iterator it = pages.begin();

        for (it; it != pages.end(); it++)
            output << (*it) << " ";
        output << "\n";
    }

    return output;
}

std::ostream& operator<<(std::ostream& output, const Dictionary& dictionary)
{
    const std::vector<Entry>& entries = dictionary.getEntries();
    std::vector<Entry>::const_iterator it = entries.begin();

    for(it; it != entries.end(); it++)
        output << (*it);

    return output;
}

void Entry::increasePage(int page)
{
    this->pages_.push_back(page);
}
